package com.aji.controller;

import com.aji.dto.*;
import com.aji.entity.CsvResponse;
import com.aji.result.PageResult;
import com.aji.result.Result;
import com.aji.service.IsotopeService;
import com.aji.vo.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/isotope")
@Api(tags = "同位素数据管理")
@Slf4j
@CrossOrigin
public class IsotopeController {
    @Autowired
    IsotopeService isotopeService;

    @ApiOperation("训练 模型")
    @GetMapping("/trainModel/{modelIds}")
    private Result trainIso(@PathVariable List<Integer> modelIds) throws IOException, InterruptedException {
        isotopeService.trainIso(modelIds);
        return Result.success();
    }

    @ApiOperation("预测 根据id")
    @GetMapping("/predict/{modelIds}/{ids}")
    public ResponseEntity<List<IsotopeRunVo>> preEle(@PathVariable List<Integer> modelIds, @PathVariable List<Integer> ids) throws IOException, InterruptedException {
        ResponseEntity<List<IsotopeRunVo>> resultList = isotopeService.testEle(modelIds, ids);
        log.info(String.valueOf(resultList));
        // 返回过滤后的结果
        return resultList; // 这个已经是 JSON 格式
    }

    @ApiOperation("查询 同位素记录，条件")
    @GetMapping
    private Result selectCon(IsotopeDto isotopeDto) {
        log.info("isotope模糊查询controller:{},{},{}", isotopeDto.toString());
        List<IsotopeVo> isotopeVoList = isotopeService.selectCon(isotopeDto);
        return Result.success(isotopeVoList);
    }

    @ApiOperation("查询 同位素记录，分页，条件")
    @GetMapping("/{page}/{pageSize}")
    private Result<PageResult> selectPage(IsotopeDto isotopeDto, @PathVariable int page, @PathVariable int pageSize) {
        log.info("isotope模糊分页查询controller:{},{},{}", isotopeDto.toString(), page, pageSize);
        PageResult pageResult = isotopeService.pageQuery(isotopeDto, page, pageSize);
        return Result.success(pageResult);
    }

    @ApiOperation("查询 同位素记录,根据id,用于页面回显")
    @GetMapping("/{id}")
    private Result selectById(@PathVariable int id) {
        IsotopeByIdVo isotopeByIdVo = isotopeService.selectById(id);
        return Result.success(isotopeByIdVo);
    }

    @ApiOperation("删除 同位素记录,批量")
    @DeleteMapping("/{ids}")
    private Result deleteByIds(@PathVariable List<Integer> ids) {
        log.info("isotope删除controller:{}", ids.toString());
        isotopeService.deleteByIds(ids);
        return Result.success();
    }

    @ApiOperation("增加 同位素记录")
    @PostMapping
    private Result add(@RequestBody IsotopeAddDto isotopeAddDto) {
        log.info("isotope添加controller:{}", isotopeAddDto);
        isotopeService.add(isotopeAddDto);
        return Result.success();
    }

    @ApiOperation("批量增加 微量元素记录")
    @PostMapping("/batch")
    private Result addBatch(@RequestBody List<IsotopeAddDto> isotopeAddDtoList){
        log.info("isotope添加controller:{}",isotopeAddDtoList);
        isotopeService.addBatch(isotopeAddDtoList);
        return Result.success();
    }
    @ApiOperation("读取 批量 同位素记录")
    @PostMapping("/upload")
    public ResponseEntity<List<IsotopeBatchVo>> uploadFile(@RequestParam("file") MultipartFile file) {
        return isotopeService.uploadFile(file);

    }


    @ApiOperation("修改 同位素记录")
    @PutMapping("/{id}")
    private Result upDate(@RequestBody IsotopeChangeDto isotopeChangeDto, @PathVariable int id) {
        log.info("isotope修改controller:{},{}", isotopeChangeDto, id);
        isotopeService.upDate(isotopeChangeDto, id);
        return Result.success();
    }
}
